package cn.school.demo.ui.party_history_answer

import android.annotation.SuppressLint
import android.graphics.Color
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.os.bundleOf
import androidx.databinding.DataBindingUtil
import androidx.navigation.fragment.findNavController
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentPartyHistoryAnswerBinding
import cn.school.demo.pojo.Subject
import cn.school.demo.pojo.Test
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.mylhyl.circledialog.CircleDialog
import java.time.LocalDateTime

class PartyHistoryAnswerFragment : Fragment() {

    private lateinit var binding: FragmentPartyHistoryAnswerBinding
    private lateinit var viewModel: PartyHistoryAnswerViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        requireActivity().window.apply {
            statusBarColor = Color.argb(0, 239, 239, 239)
            decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        viewModel = ViewModelProvider(this).get(PartyHistoryAnswerViewModel::class.java)
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_party_history_answer,
            container,
            false
        )
        binding.vw = this
        binding.lifecycleOwner = viewLifecycleOwner
        return binding.root
    }

    fun gotoMy() = MyApplication.goto(
        R.id.action_PartyHistoryAnswer_to_My,
        findNavController(),
        parentFragmentManager
    )

    fun partyHistoryAnswerHistoryOnClick() {
        MyApplication.navigateThing["History"] =
            R.id.action_History_to_PartyHistoryAnswer.toString()
        MyApplication.goto(
            R.id.action_PartyHistoryAnswer_to_History,
            findNavController(),
            requireFragmentManager()
        )
    }

    @SuppressLint("NewApi")
    fun gotoAnswer(id: Int) {
        MyApplication.isSubject = false
        viewModel.apply {
            var map: Map<String, String> = mapOf()
            when (id) {
                viewModel.DAILYQUIZ -> {
                    getDayResult(parentFragmentManager)
                    map = mapOf("title" to "每日答题", "introduction" to "太阳每一天都是新的！")
                    MyApplication.answerTopic = "每日党史答题"
                }
                viewModel.WEEKLYQUIZ -> {
                    getWeekResult(parentFragmentManager)
                    map = mapOf("title" to "每周答题", "introduction" to "周而复始，如期而至！")
                    MyApplication.answerTopic = "每周党史答题"
                }
                viewModel.SPECIALQuestion -> {
                    map = mapOf("title" to "专项答题", "introduction" to "术业有专攻，道义有精论！")
                    MyApplication.answerTopic = "专项党史答题"
                    MyApplication.goto(
                        R.id.action_PartyHistoryAnswer_to_Professional,
                        findNavController(),
                        parentFragmentManager
                    )
                }
            }
            if (id == viewModel.DAILYQUIZ || id == viewModel.WEEKLYQUIZ)
                testList.observe(viewLifecycleOwner) {
                    val bundle: Bundle = bundleOf(Pair("map", GsonUtils.toJson(map)))
                    MyApplication.apply {
                        testList.apply {
                            clear()
                            viewModel.testList.value!!.forEach {
                                add(it)
                            }
                        }
                        LogUtils.e(testList.size)
                        subjectList.apply {
                            clear()
                            add(
                                Subject(
                                    1,
                                    1,
                                    "",
                                    "",
                                    "",
                                    "",
                                    "",
                                    "",
                                    "",
                                    "",
                                    "",
                                    "",
                                    LocalDateTime.now().toString(),
                                    LocalDateTime.now().toString(),
                                    1
                                )
                            )
                        }
                        navigateThing["AnswerMain"] =
                            R.id.action_AnswerMain_to_PartyHistoryAnswer.toString()
                        navigateThing["Answer"] =
                            R.id.action_Answer_to_PartyHistoryAnswer.toString()
                        goto(
                            R.id.action_PartyHistoryAnswer_to_Answer,
                            findNavController(),
                            parentFragmentManager,
                            bundle
                        )
                    }
                }
        }
    }
}